package org.dreamwork.assistant.tools.network.gui;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by seth.yang on 2018/5/9
 */
public class UdpAdapter implements INetworkAdapter {
    private DatagramSocket socket;
    private InetAddress remote;
    private int port;

    UdpAdapter (DatagramSocket socket, DatagramPacket packet) {
        this.socket = socket;
        remote      = packet.getAddress ();
        port        = packet.getPort ();
    }

    @Override
    public void write (byte[] data) throws IOException {
        if (socket != null) {
            DatagramPacket packet = new DatagramPacket (data, data.length, remote, port);
            socket.send (packet);
        }
    }

    @Override
    public void close () {
/*
        if (socket != null) {
            socket.close ();
        }
*/
    }
}
